import moment from 'moment';
import { baseEcharts, RADIOCHECK, DATATYPE, TIMEOBJ, TIME_ENUM } from '../Adjust/helper';

/* 通用y轴配置 */
const yAxisConfig = {
  type: 'value',
  axisLabel: {
    show: true,
    color: 'rgba(216, 220, 230, 0.4)',
  },
  axisLine: {
    show: false,
  },
  axisPointer: {
    label: {
      backgroundColor: '#444444',
      color: 'white',
      shadowBlur: 0,
    },
  },
  splitLine: { lineStyle: { color: '#363636' } },
};

/* 通用echarts配置 */
const generalConfig = (data, toolFn) => {
  let legendData = data?.map((item) => {
    return {
      name: item?.name || '',
      itemStyle: {
        color: '#272829',
        borderColor: item?.color || '',
        borderWidth: 1,
      },
    };
  });
  return {
    xAxis: {
      type: 'time',
      axisLabel: {
        color: 'rgba(255,255,255,0.5)',
        fontSize: 12,
        formatter: (val) => moment(val).format('HHmm'),
      },
      axisTick: { show: false },
    },
    grid: { top: 20, left: 50, right: 60, bottom: 60 },
    legend: {
      bottom: 5,
      textStyle: { color: 'rgba(255,255,255,0.5)', fontSize: 12 },
      data: legendData,
      inactiveColor: '#545454',
      inactiveBorderColor: '#545454',
      inactiveBorderWidth: 'inherit',
    },
    dataZoom: [
      {
        type: 'slider',
        right: 50,
        height: 14,
        bottom: '10%',
        left: '20',
        show: false,
        minSpan: 1,
        throttle: 50,
        dataBackground: {
          areaStyle: {
            color: '#757575',
          },
        },
      },
      {
        type: 'inside',
        show: false,
        minSpan: 1,
        throttle: 500,
      },
    ],
    yAxis: { type: 'value' },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#444444',
          color: '#888E95',
          shadowBlur: 0,
        },
      },
      formatter: (val) => {
        toolFn(val);
        return;
      },
    },
  };
};

/*  格式化00:00变为24:00 */
const findCharts = (val, index) => {
  if (moment(Number(val)).format('HHmm') === '0000') {
    return moment(Number(val)).add(-1, 'days').format('YYYYMMDD') + ' 2400';
  } else {
    return moment(Number(val)).format('YYYYMMDD HHmm');
  }
};
/* 生成96点区间 */
export const generateNum = (begin, end) => {
  if (begin === '' || end === '') {
    return [];
  }
  let arr = [];
  for (let i = Number(begin); i <= Number(end); i++) {
    arr.push(i);
  }
  return arr;
};

/**排除 新能源预测-XZ 数据 后期可能会用，可以删掉*/
const ENERGY = '新能源预测-XZ';

/* 供需分析配置 */
const supplyAndCharts = (datas, toolFn) => {
  const data = datas.filter((i) => i.name !== ENERGY);
  const nameArr = ['系统负荷-联络线', '调整后-出力预测'];
  let xAxisData = [];
  let _data = data.map((item, index) => {
    _data = item?.list?.map((item, index) => {
      if (xAxisData.length < 96) {
        xAxisData.push(findCharts(item.xAxis));
      }
      return {
        ...item,
        xAxis: findCharts(item.xAxis, index),
      };
    });
    return {
      ...item,
      data: _data,
      areaStyle: {
        color: nameArr.includes(item?.name) ? '' : item?.color || '',
        opacity: nameArr.includes(item?.name) ? 0 : 0.2,
      },
      stack: nameArr.includes(item?.name) ? index : 1,
      symbolSize: 0,
      symbol: 'circle',
      smooth: true,
      itemStyle: {
        normal: {
          lineStyle: {
            width: 1, //设置线条粗细
          },
        },
      },
    };
  });
  _data = _data.filter((item) => item.type === 'line');
  return {
    ...generalConfig(data, toolFn),
    grid: { top: 20, left: 50, right: 10, bottom: 60 },
    yAxis: {
      ...yAxisConfig,
    },
    xAxis: {
      show: true,
      data: xAxisData,
    },
    series: _data,
  };
};

/* 调整比例配置 */
const adjustCharts = (data, toolFn) => {
  let obj = {
    dataSource: [],
    fixation: [],
  };
  let maxValue = 0;
  data?.forEach((item) => {
    if (item?.useType === RADIOCHECK['DATASOURCE']) {
      obj.dataSource.push(item);
    } else {
      obj.fixation.push(item);
    }
  });
  let echartsData = baseEcharts.map((item) => {
    let brr = [];
    let _data = item.key != DATATYPE['FIXEDVALUE'] ? obj.dataSource : obj.fixation;
    _data.forEach((val) => {
      let startIndex = TIME_ENUM.findIndex((bal) => bal === val?.[TIMEOBJ['BEGIN']]);
      let endIndex = TIME_ENUM.findIndex((bal) => bal === val?.[TIMEOBJ['END']]);
      let _arr = generateNum(startIndex, endIndex);
      if (startIndex == -1 || endIndex == -1) {
        _arr = [];
      }
      _arr = _arr.map((bal) => {
        return {
          xAxis: bal,
          value: Number(val[item.key]),
        };
      });
      brr = [...brr, ..._arr];
    });
    let _brr = generateNum(0, 95);
    _brr = _brr.map((val) => {
      let result = brr.find((cal) => cal.xAxis == val)?.value;
      if (result && Number(maxValue) < Number(result) && item.key === DATATYPE['RATEVALUE']) {
        maxValue = result;
      }

      return {
        xAxis: TIME_ENUM?.[val] || '',
        value: result,
      };
    });
    return {
      ...item,
      type: 'line',
      symbol: item?.key === DATATYPE['RATEVALUE'] ? 'diamond' : 'circle',
      step: false,
      symbolSize: 6,
      lineStyle: { color: item?.color || '' },
      yAxisIndex: item?.key === DATATYPE['RATEVALUE'] ? 1 : 0,
      data: _brr,
      itemStyle: {
        normal: {
          lineStyle: {
            width: 1, //设置线条粗细
          },
        },
      },
    };
  });
  return {
    ...generalConfig(data, toolFn),
    legend: {
      data: echartsData.map((item) => ({
        name: item?.name,
        color: 'white',
        itemStyle: { opacity: 1, borderWidth: 1, borderColor: item.color, color: '#1F1F1F' },
      })),
      textStyle: {
        color: 'white',
      },
      show: true,
      bottom: 0,
      inactiveColor: '#545454',
      inactiveBorderColor: '#545454',
      inactiveBorderWidth: 'inherit',
    },
    xAxis: {
      type: 'category',
      data: TIME_ENUM,
      axisLabel: {
        show: true,
        color: 'rgba(216, 220, 230, 0.4)',
      },
      interval: 12,
      axisTick: {
        show: true,
      },
      axisPointer: {
        label: {
          backgroundColor: '#444444',
          color: 'white',
          shadowBlur: 0,
        },
      },
      boundaryGap: false,
    },
    yAxis: [
      {
        ...yAxisConfig,
      },
      {
        ...yAxisConfig,
        splitLine: {
          show: false,
        },
        axisLabel: {
          formatter: `{value}%`,
          color: 'rgba(216, 220, 230, 0.4)',
        },
        axisLine: {
          show: false,
        },
        max: maxValue * 1 + (maxValue * 1 < 100 ? 10 : 100),
      },
    ],
    series: echartsData,
  };
};

/* 抛出 */
export function getCharts(key, data, toolFn) {
  switch (key) {
    case 'SUPPLY_AND_DEMAND':
      return supplyAndCharts(data, toolFn);
    case 'ADJUST_SCALE':
      return adjustCharts(data, toolFn);
  }
}
